package day8;

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.text.AbstractDocument.BranchElement;

import org.junit.Test;

public class StudentManager {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		ArrayList<Student> list = new ArrayList<Student>();

		while (true) {
			System.out.println("请输入学生id：");
			int id = scanner.nextInt();
			System.out.println("请输入学生姓名：");
			scanner.nextLine();
			String name = scanner.next();
			System.out.println("请输入学生年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入学生性别：");
			String gender = scanner.next();
			Student student = new Student(id, name, age, gender);
			list.add(student);

			System.out.println("是否继续输入：0：退出 1：继续");
			int quit = scanner.nextInt();
			if (quit == 0) {
				break;
			}
		}

		System.out.println("---------------");

		for (Student student : list) {
			System.out.println(student);
		}

		System.out.println("---------------");

		// 1.用户搜索完了之后可以不停的搜索
		// 2.搜索过程中有这个学生年龄信息，就打印这个对象信息
		// 3.如果搜索完了之后，没有找到符合条件的学生信息，就打印“没有该信息的学生”
		while (true) {
			System.out.println("----------------------------");
			System.out.println("输入1，按id搜索");
			System.out.println("输入2，按年龄搜索");
			System.out.println("输入3，按名字搜索");
			System.out.println("输入4，按性别搜索");
			System.out.println("输入0，退出");
			System.out.println("----------------------------");
			int type = scanner.nextInt();
			if (type == 0) {
				System.out.println("退出程序");
				break;
			}

			// boolean isSearchFound = false;
			switch (type) {
			case 1:
				System.out.println("请输入您要查找的id：");
				int id = scanner.nextInt();
				searchById(id, list);
				break;
			case 2:
				System.out.println("请输入您要查找的年龄：");
				int age = scanner.nextInt();
				searchByAge(age, list);
				break;
			case 3:
				System.out.println("请输入您要查找的名字：");
				String name = scanner.next();
				searchByName(name, list);
				break;
			case 4:
				System.out.println("请输入您要查找的性别：");
				String gender = scanner.next();
				searchByGender(gender, list);
				break;
			default:
				break;
			}
		}
		scanner.close();
	}

	private static void searchById(int id, ArrayList<Student> list) {
		// 初始认为根据id没有找到这个学生，因为还没有开始找，所以初始没有找到
		boolean isSearchByIdFound = false;
		for (Student student : list) {
			if (id == student.getId()) {
				System.out.println(student);
				isSearchByIdFound = true;
			}
		}
//				if (!isSearchByIdFound) {
//					
//				}
		if (isSearchByIdFound == false) {
			System.out.println("没有该信息的学生");
		}
	}

	private static void searchByAge(int age, ArrayList<Student> list) {
		boolean isSearchByAgeFound = false;
		for (Student student : list) {
			if (age == student.getAge()) {
				System.out.println(student);
				isSearchByAgeFound = true;
			}
		}
		if (isSearchByAgeFound == false) {
			System.out.println("没有该信息的学生");
		}
	}

	private static void searchByGender(String gender, ArrayList<Student> list) {
		boolean isSearchByGenderFound = false;
		for (Student student : list) {
			if (gender.equals(student.getGender())) {
				System.out.println(student);
				isSearchByGenderFound = true;
			}
		}
		if (isSearchByGenderFound == false) {
			System.out.println("没有该信息的学生");
		}
	}

	private static void searchByName(String name, ArrayList<Student> list) {
		boolean isSearchByNameFound = false;
		for (Student student : list) {
			if (name.equals(student.getName())) {
				System.out.println(student);
				isSearchByNameFound = true;
			}
		}
		if (isSearchByNameFound == false) {
			System.out.println("没有该信息的学生");
		}
	}

}
